package dao;

import entity.Employ;
import util.Oprea;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Map;

public class EmployDaoImpl implements EmployInterface {
    Map<Integer,Employ>employees=Init.employMap;
    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
    File file=new File("employ.txt");
    @Override
    public void show() {
        Map map= Oprea.unseria(file);
        if(map!=null){
            employees=map;
        }
        Collection<Employ>employs=employees.values();
        System.out.println("员工编号\t员工姓名\t员工职位\t员工入职时间\t员工工资");
        for(Employ employ:employs){
            System.out.println(employ.getID()+"\t"+employ.getName()+"\t"+employ.getJob()+"\t"+simpleDateFormat.format(employ.getDate())+"\t"+employ.getMoney());
        }
    }

    @Override
    public void showByID(int ID) {
        Map map=Oprea.unseria(file);
        if(map!=null){
            employees=map;
        }
        if(employees.containsKey(ID)){
            Employ employ=employees.get(ID);
            System.out.println("员工编号\t员工姓名\t员工职位\t员工入职时间\t员工工资");
            System.out.println(employ.getID()+"\t"+employ.getName()+"\t"+employ.getJob()+"\t"+simpleDateFormat.format(employ.getDate())+"\t"+employ.getMoney());
        }
    }

    @Override
    public boolean addEmp(Employ employ) {
        Map map=Oprea.unseria(file);
        if(map!=null){
            employees=map;
        }
        employees.put(employ.getID(),employ);
        Oprea.seria(employees,file);
        return true;
    }

    @Override
    public boolean deleteEmp(int ID) {
        Map map=Oprea.unseria(file);
        if(map!=null){
            employees=map;
        }
        employees.remove(ID);
        Oprea.seria(employees,file);
        return true;
    }

    @Override
    public boolean updateEmp(Employ employ) {
        Map map=Oprea.unseria(file);
        if(map!=null){
            employees=map;
        }
        if(employees.containsKey(employ.getID())){
          Employ employ1=employees.get(employ.getID());
          employ1.setMoney(employ.getMoney());
        }
        Oprea.seria(employees,file);
        return true;
    }
}
